# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ghostscript
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.53.3
pkgrel=1
arch=('any')
license=('AGPL' 'custom')
pkgdesc="An interpreter for the PostScript language (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-dbus"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-jbig2dec"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libidn"
         "${MINGW_PACKAGE_PREFIX}-libpaper"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         #"${MINGW_PACKAGE_PREFIX}-lcms2" # Use ghostscript's fork of LCMS2 as it patched to be threadsafe
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-gnutls")
optdepends=("${MINGW_PACKAGE_PREFIX}-gtk3: needed for gsx")
url="https://www.ghostscript.com/"
source=(https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs${pkgver//./}/ghostscript-${pkgver}.tar.xz
        001-mingw-build.patch
        002-ghostscript-sys-zlib.patch
        003-libspectre.patch
        004-FT_CALLBACK_DEF-deprecated.patch)
options=('staticlibs' 'strip')
sha256sums=('9c9f5bc85b6c7eb08368c05b1e3339f7aaf9677ddca710c6283f872d55e2a234'
            '4638b24bb6e399e67e18f82317be1b54aa9bb87a0ae3b60585b607d529cb0342'
            '9cf9805e6b8329e83656c16475c9fc7b7b8c3e459a782b4755abdabbb8f22df4'
            '5b0ef04d59658885f183a6c820a5dbbb89e57e4c43774bc6c90ae4fb742b2be7'
            'e338e4b6877119acdc2899e4d89b9b31e3993dc0564f16744e320789387eebf9')
noextract=(ghostscript-${pkgver}.tar.xz)

prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xJf ${srcdir}/${_realname}-${pkgver}.tar.xz -C ${srcdir} || true

  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-mingw-build.patch
  patch -p1 -i ${srcdir}/002-ghostscript-sys-zlib.patch
  patch -p1 -i ${srcdir}/003-libspectre.patch
  patch -p1 -i ${srcdir}/004-FT_CALLBACK_DEF-deprecated.patch

  sed -e "s|/usr|${MINGW_PREFIX}/${MINGW_CHOST}|g" -i base/all-arch.mak base/unix-aux.mak

  # force it to use system-libs
  # openjpeg use included
  rm -rf expat freetype jbig2dec jpeg libpng openjpeg tiff zlib

  #cd base
  libtoolize --force --copy --install
  autoconf -f -i
}

build()
{
  cd ${srcdir}/${_realname}-${pkgver}
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --disable-contrib \
    --enable-fontconfig \
    --enable-dbus \
    --enable-freetype \
    --disable-cups \
    --enable-openjpeg \
    --enable-gtk \
    --with-jbig2dec \
    --with-libiconv=gnu \
    --with-libidn \
    --with-libpaper \
    --with-system-libtiff \
    --without-x \
    --with-drivers=ALL

  make so
  #make gs.a
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}
  make DESTDIR="${pkgdir}" soinstall

  #install -D -m644 "${srcdir}"/${_realname}-${pkgver}/gs.a "${pkgdir}"/${MINGW_PREFIX}/lib/libgs.a

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/${_realname}/${pkgver}/
  cp -rf "${srcdir}"/${_realname}-${pkgver}/doc "${pkgdir}${MINGW_PREFIX}"/share/${_realname}/${pkgver}/

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  cp ${pkgdir}${MINGW_PREFIX}/bin/{gsc,gswin32c}.exe
  cp ${pkgdir}${MINGW_PREFIX}/bin/{gsc,gs}.exe

  # remove unwanted localized man-pages
  rm -rf "${pkgdir}${MINGW_PREFIX}"/share/man/[^man1]*
}
